package com.barunsw.bwf.ems.neoarena.ne.tamms.ast;


public class AstControl implements ASTConstants {
	final int   ERROR_LENGTH = 128;
	java.io.DataInputStream stream = null;

	public AstControl(char[] data) {
		try {
			byte[] newData = new byte[data.length];
			for ( int i = 0; i < data.length; i++ )
				newData[i] = (byte)data[i];

			stream = new java.io.DataInputStream(new java.io.ByteArrayInputStream(newData));

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public M4101_MODE getDisTestCall(){
		M4101_MODE mode = new M4101_MODE();

		try{
			mode.rCnt = stream.readInt();
			for(int i = 0; i <  mode.M4101_MAX_ITEM; i++)
				mode.rCallerLn[i] = stream.readInt();
			
			for(int i = 0; i <  mode.M4101_MAX_ITEM; i++)
				mode.rCallingLn[i] = stream.readInt();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M4122_MODE getDisTestMtv(){
		M4122_MODE mode = new M4122_MODE();
		byte[] msg = new byte[6];

		try{
			mode.devType = stream.readShort();
			for(int i = 0; i <  mode.M4122_MAX_ITEM; i++){
				mode.infItem[i] = new M4122_ITEM();

 				stream.read(msg, 0, msg.length);
				mode.infItem[i].min = (new String(msg)).trim();
 				stream.read(msg, 0, msg.length);
				mode.infItem[i].max = (new String(msg)).trim();
 				stream.read(msg, 0, msg.length);
				mode.infItem[i].mid = (new String(msg)).trim();
 				stream.read(msg, 0, msg.length);
				mode.infItem[i].def_min = (new String(msg)).trim();
 				stream.read(msg, 0, msg.length);
				mode.infItem[i].def_max = (new String(msg)).trim();
 				stream.read(msg, 0, msg.length);
				mode.infItem[i].def_mid = (new String(msg)).trim();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M4123_MODE getDisTestSubs(){
		M4123_MODE mode = new M4123_MODE();

		try{
			mode.cnt = stream.readInt();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> cnt : " + mode.cnt);
			for(int i = 0; i <  mode.M4123_MAX_ITEM; i++){
				mode.infItem[i] = new M4123_ITEM();
				mode.infItem[i].jobNo = stream.readInt();
				mode.infItem[i].cid = stream.readInt();
				mode.infItem[i].lid = stream.readInt();
				mode.infItem[i].devItem = stream.readShort();
				stream.readShort();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}

	public M4124_MODE getDisTestBus(){
		M4124_MODE mode = new M4124_MODE();

		try{
			mode.cnt = stream.readInt();
			for(int i = 0; i <  mode.M4124_MAX_ITEM; i++){
				mode.infItem[i] = new M4124_ITEM();
				mode.infItem[i].bus_no = stream.readInt();
				mode.infItem[i].bus_sts = stream.readShort();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return mode;
	}


	public String getErrorReason()
	{
		byte[] err = new byte[ERROR_LENGTH];
		try
		{
			stream.read(err, 0, err.length);
			return (new String(err)).trim();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		finally
		{
			try
			{
				stream.close();
			}catch(Exception e1){}
		}
	}

	public int close()
	{
		try
		{
			stream.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
}
